package com.spring.sunflower.conversation;

import com.blankj.utilcode.util.ToastUtils;
import com.spring.sunflower.App;
import com.spring.sunflower.base.BaseObserver;
import com.spring.sunflower.base.BasePresenter;
import com.spring.sunflower.bean.AccountBean;
import com.spring.sunflower.bean.AnchorDetailBean;
import com.spring.sunflower.bean.BlackHasBean;
import com.spring.sunflower.bean.CommonBean;
import com.spring.sunflower.bean.LabelBean;
import com.spring.sunflower.bean.LiveApplyChatBean;
import com.spring.sunflower.bean.STSCertBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.DialogManager;
import com.spring.sunflower.encrypt.Constants;
import com.spring.sunflower.util.AppUtils;
import com.spring.sunflower.util.SPUtils;

import io.reactivex.rxjava3.core.Observable;

/**
 * Created by Kevin on 2021/6/5<br/>
 * <p>
 * Describe:<br/>
 */
public class ChatPresenter extends BasePresenter<ConversationView> {
    public ChatPresenter(ConversationView baseView) {
        super(baseView);
    }

    public void getSTSCert() {
        Observable<STSCertBean> observable = requestDataSource.getSTSCert();
        addDisposable(observable, new BaseObserver<STSCertBean>(baseView) {

            @Override
            public void onSuccess(STSCertBean bean) {
                STSCertBean.DatasBean datas = bean.getDatas();
                baseView.loadSTSCert(datas);
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    /**
     * @param userId   本人id
     * @param anchorId 对方id
     */
    public void applyChat(String userId, String anchorId) {
        String gender = getField(Constant.KEY_GENDER, "1");
        Observable<LiveApplyChatBean> observable;
        if ("1".equals(gender)) {
            observable = requestDataSource.liveApplyChat(userId, anchorId);
        } else {
            observable = requestDataSource.liveApplyChatAnchor(anchorId);
        }
        addDisposable(observable, new BaseObserver<LiveApplyChatBean>(baseView) {
            @Override
            public void onSuccess(LiveApplyChatBean bean) {
                baseView.liveApplySuccess();
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }
    public void getHasBlacked(String blackMemberId, String operatorId) {
        Observable<BlackHasBean> observable = requestDataSource.requestHasBlack(blackMemberId, operatorId);
        addDisposable(observable, new BaseObserver<BlackHasBean>(baseView) {

            public void onSuccess(BlackHasBean bean) {
                String datas = bean.getDatas().getData();
                if ("1".equals(datas)) {
                    baseView.hasBlacked(true);
                } else {
                    baseView.hasBlacked(false);
                }
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    public void getAccount() {
        Observable<AccountBean> observable = requestDataSource.getAccount();
        addDisposable(observable,new BaseObserver<AccountBean>(){
            @Override
            public void onSuccess(AccountBean bean) {
                baseView.showAccount(bean.getDatas());
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }
    public void getAnchorDetail(String targetId) {
        Observable<AnchorDetailBean> observable = requestDataSource.requestAnchorDetail(targetId);
        addDisposable(observable, new BaseObserver<AnchorDetailBean>(baseView) {

            @Override
            public void onSuccess(AnchorDetailBean bean) {
                AnchorDetailBean.DatasBean datas = bean.getDatas();
//                baseView.hideLoading();
                baseView.showAnchorData(datas);
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }
    public void getHasFollowed(String targetId) {
        Observable<CommonBean> observable = requestDataSource.requestFollowedHas(targetId);
        addDisposable(observable, new BaseObserver<CommonBean>() {

            @Override
            public void onSuccess(CommonBean bean) {
//
                String datas = bean.getDatas();
                if(!AppUtils.isVerify(App.getContext())) {
                    if ("1" == datas) {
                        baseView.hasFollowed(true);
                    } else {
                        baseView.hasFollowed(false);
                    }
                }else{
                    if(targetId.equals("10001")) {
                        baseView.hasFollowed(false);
                    }
                }
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }

    public void setFollowed(String targetId, String set) {
        Observable<CommonBean> observable = requestDataSource.requestFollowedSet(targetId, set);
        addDisposable(observable, new BaseObserver<CommonBean>() {

            @Override
            public void onSuccess(CommonBean bean) {
                baseView.setFollowedSuccess();
            }

            @Override
            public void onError(int status, String msg) {
                baseView.setFollowedFailed();
            }
        });
    }

    public void getVisualizeLabel() {
        Observable<LabelBean> observable = requestDataSource.requestLabel("customerImTips");
        addDisposable(observable, new BaseObserver<LabelBean>(baseView) {

            @Override
            public void onSuccess(LabelBean bean) {
                String name = bean.getDatas().get(0).getName();
                SPUtils.setSP("customerImTips",App.getContext(),name);
                DialogManager.getInstance().showCustomerDialog(ChatActivity.getSingleton,null);
            }

            @Override
            public void onError(int status, String msg) {

            }
        });
    }
}
